//
//  UserDB.m
//  数据增删查改
//
//  Created by app05 on 15/7/21.
//  Copyright (c) 2015年 app. All rights reserved.
//

#import "UserDB.h"


static UserDB *instance;


@implementation UserDB


+(id)shareInstance
{
    if (instance==nil) {
        instance=[[[self class] alloc]init];
    }
    return instance;
}
-(void)creatTable
{
    NSString *sql=@"create table if not exists User (username TEXT primary key,telephone TEXT,bigloca TEXT,littleloca TEXT,postnum TEXT);";
    [self creatTable:sql];
}

-(BOOL)addUser:(UserModel *)userModel
{
    NSString *sql=@"insert or replace into User(username,telephone,bigloca,littleloca,postnum) values(?,?,?,?,?)";

    NSArray *params=[NSArray arrayWithObjects:userModel.userName,userModel.telephone,userModel.bigLoca,userModel.littleLoca,userModel.postNum, nil];
    
    return  [self dealData:sql paramsarray:params];
}

-(BOOL)updataUser:(UserModel *)userModel
{
    NSString *sql=@"update User set telephone=?,bigloca=?,littleloca=?,postnum=? where username=?";
    
    NSArray *params=[NSArray arrayWithObjects:userModel.telephone,userModel.bigLoca,userModel.littleLoca,userModel.postNum,userModel.userName, nil];
    
    return  [self dealData:sql paramsarray:params];
}
-(BOOL)deletedUser:(UserModel *)userModel
{
    NSString *sql=@"delete from User where username=?";
    
    NSArray *params=[NSArray arrayWithObjects:userModel.userName, nil];
    
    return  [self dealData:sql paramsarray:params];
}
-(NSArray *)findUsers
{
    NSString *sql=@"select username,telephone,bigloca,littleloca,postnum from User";
    
    NSArray *data=[self selectData:sql column:5];
    
    NSMutableArray *users=[NSMutableArray array];
    for (NSArray *row in data) {
        NSString *username=[row objectAtIndex:0];
        NSString *telephone=[row objectAtIndex:1];
        NSString *bigLoca=[row objectAtIndex:2];
        NSString *littleLoca=[row objectAtIndex:3];
        NSString *postNum=[row objectAtIndex:4];
        
        UserModel *userModel=[[UserModel alloc]init];
        userModel.userName=username;
        userModel.telephone=telephone;
        userModel.bigLoca=bigLoca;
        userModel.littleLoca=littleLoca;
        userModel.postNum=postNum;
        
        [users addObject:userModel];
    }
    return users;
}
@end
